package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.R;
import com.itheima.dao.CategoryDao;
import com.itheima.exception.MyException;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryDao categoryDao;
    @Override
    @Transactional
    public R deleteCategory(Long id) {

        LambdaQueryWrapper<Dish> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(id!=null,Dish::getCategoryId,id);
       int count1 = dishService.count(wrapper);
        if (count1!=0){
//            return R.error("该分类有菜品无法删除！");
            throw new RuntimeException("该分类有菜品无法删除！");
        }

        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count = setmealService.count(lambdaQueryWrapper);
        if (count!=0){

//            return R.error("该分类下有套餐无法删除!");
        throw new RuntimeException("该分类下有套餐无法删除!");
        }
        categoryDao.deleteById(id);


        return R.success("删除成功！");

    }
}
